package com.game.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;

import com.game.business.service.HxSevice;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.enums.BusinessType;
import com.game.common.utils.StringUtils;
import com.game.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.business.domain.GroupMember;
import com.game.business.service.IGroupMemberService;
import com.game.common.core.page.TableDataInfo;

/**
 * 群聊成员Controller
 *
 * @author ruoyi
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/business/groupMember")
public class GroupMemberController extends BaseController {
    @Autowired
    private IGroupMemberService groupMemberService;

/**
 * 查询群聊成员列表
 */
//@RequiresPermissions("business:groupMember:list")
@GetMapping("/list")
    public TableDataInfo list(GroupMember groupMember) {
        startPage();
        List<GroupMember> list = groupMemberService.selectGroupMemberList(groupMember);
        return getDataTable(list);
    }

    /**
     * 导出群聊成员列表
     */
    //@RequiresPermissions("business:groupMember:export")
    @Log(title = "群聊成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GroupMember groupMember) {
        List<GroupMember> list = groupMemberService.selectGroupMemberList(groupMember);
        ExcelUtil<GroupMember> util = new ExcelUtil<GroupMember>(GroupMember. class);
        util.exportExcel(response, list, "群聊成员数据");
    }

    /**
     * 获取群聊成员详细信息
     */
    //@RequiresPermissions("business:groupMember:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(groupMemberService.getById(id));
    }
    @Autowired
    private HxSevice hxSevice;
    /**
     * 新增群聊成员
     */
    //@RequiresPermissions("business:groupMember:add")
    @Log(title = "群聊成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GroupMember vo) {
        String groupId = vo.getGroupId();
        String userId = vo.getUserId();
        if(StringUtils.isEmpty(groupId) || StringUtils.isEmpty(userId)){
            return error("参数错误");
        }
        // 添加环信一个成员
        hxSevice.addMembersToGroup(groupId,userId);
        groupMemberService.save(vo);
        return AjaxResult.success();
    }

    /**
     * 修改群聊成员
     */
    //@RequiresPermissions("business:groupMember:edit")
    @Log(title = "群聊成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GroupMember groupMember) {
        return toAjax(groupMemberService.updateById(groupMember));
    }

    /**
     * 删除群聊成员
     */
    //@RequiresPermissions("business:groupMember:remove")
    @Log(title = "群聊成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            GroupMember byId = groupMemberService.getById(ids[i]);
            String groupId = byId.getGroupId();
            hxSevice.removeUserFromGroup(groupId, byId.getUserId());
            groupMemberService.removeById(ids[i]);
        }
        return AjaxResult.success();
    }
}
